﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDataBaseWork;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.Query;
using XF.Meeting.IDal;

namespace XF.Meeting.DAL
{
    public class GroupDal : BaseDAL, IGroupDal
    {
        public Task<DataTable> GetGroupList(out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetGroupList", out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetGroupList EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult(ds.Tables[0]);
        }


        /// <summary>
        /// 根据群组ID获取当前群组下所有人员信息
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, int, int>> GetUserListByGroupIdAndKey(GroupQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@GroupID",query.GroupID),
                  
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserListByGroupIdAndKey", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserListByGroupIdAndKey EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            var tup = new Tuple<DataTable, int, int>(ds.Tables[0], TotalRowsCount, TotalPageCount);
            return Task.FromResult(tup);
        }

        /// <summary>
        /// 新建群组
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> AddGroup(GroupInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@GroupName",entity.GroupName),
                  new SqlParameter("@CreatedUserID",entity.CreatedUserID),
                  //new SqlParameter("@Level",entity.Level),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spInsertGroup", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spInsertGroup EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 删除群组
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> DeleteGroup(GroupInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@GroupID",entity.GroupID),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);

            if (!_db.ExcuteByTransaction(paras, "spDeleteGroup", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spDeleteGroup EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 修改群组
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateGroup(GroupInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@GroupID",entity.GroupID),
                  new SqlParameter("@GroupName",entity.GroupName),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);

            if (!_db.ExcuteByTransaction(paras, "spUpdateGroup", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateGroup EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 群组添加User
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> AddGroupUser(GroupInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@GroupId",entity.GroupID),
                  new SqlParameter("@UserIds",entity.UserIds),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spInsertGroupUser", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spInsertGroup EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 群组修改删除User
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateGroupUser(GroupInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@GroupId",entity.GroupID),
                  new SqlParameter("@UserIds",entity.UserIds),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateGroupUser", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateGroupUser EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        public Task<(bool, int)> RemoveUser(GroupInfo obj, out string strError)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 获取群组根节点信息
        /// </summary>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(DataTable, int, int,bool)> GetGroupOne(GroupQuery query, out string strError)
        {
            bool IsHaveNext = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                 new SqlParameter("@Key",query.Key),
                 new SqlParameter("@CreatedUserID",query.CreatedUserID),
                 new SqlParameter("@PageIndex",query.pageIndex),
                 new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetGroupOne", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetGroupOne EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            if (TotalRowsCount > query.pageIndex * query.pageSize)
            {
                IsHaveNext = false;
            }
            else
            {
                IsHaveNext = true;
            }
            return Task.FromResult((ds.Tables[0], TotalRowsCount, TotalPageCount, IsHaveNext));
        }

        /// <summary>
        /// 根据父ID获取群组信息
        /// </summary>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<DataTable> GetGroupByPID(GroupQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                 new SqlParameter("@ParentID",query.ParentID),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetGroupByPID", out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetGroupByPID EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult(ds.Tables[0]);
        }


    }
}
